package com.pzj.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzj.dormitory.entity.*;
import com.pzj.dormitory.form.SearchForm;
import com.pzj.dormitory.mapper.*;
import com.pzj.dormitory.service.AbsentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzj.dormitory.vo.AbsentVO;
import com.pzj.dormitory.vo.MoveoutVO;
import com.pzj.dormitory.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@Service
public class AbsentServiceImpl extends ServiceImpl<AbsentMapper, Absent> implements AbsentService {
    @Autowired
    private AbsentMapper absentMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private DormitoryAdminMapper dormitoryAdminMapper;

    @Override
    public PageVO list(Integer pageNum, Integer pageSize) {
        Page<Absent> absentPage = new Page<>(pageNum,pageSize);
        Page<Absent> page = absentMapper.selectPage(absentPage, null);
        ArrayList<AbsentVO> absentVOS = new ArrayList<>();
        for (Absent absent : page.getRecords()) {
            AbsentVO absentVO = new AbsentVO();
            BeanUtils.copyProperties(absent, absentVO);
            Student student = studentMapper.selectById(absent.getStudentId());
            Dormitory dormitory = dormitoryMapper.selectById(absent.getDormitoryId());
            Building building = buildingMapper.selectById(absent.getBuildingId());
            DormitoryAdmin dormitoryAdmin = dormitoryAdminMapper.selectById(absent.getDormitoryAdminId());
            absentVO.setBuildingName(building.getName());
            absentVO.setDormitoryName(dormitory.getName());
            absentVO.setStudentName(student.getName());
            absentVO.setDormitoryAdminName(dormitoryAdmin.getName());
            absentVOS.add(absentVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setTotal(page.getTotal());
        pageVO.setData(absentVOS);
        return pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
        Page<Absent> absentPage = new Page<>(searchForm.getPage(),searchForm.getSize());
        Page<Absent> page = null;
        if (searchForm.getValue().equals("")){
            page = absentMapper.selectPage(absentPage,null);
        }else {
            QueryWrapper<Absent> absentQueryWrapper = new QueryWrapper<>();
            if(searchForm.getKey().equals("buildingName")){
                QueryWrapper<Building> buildingQueryWrapper = new QueryWrapper<>();
                buildingQueryWrapper.like("name", searchForm.getValue());
                List<Building> buildings = buildingMapper.selectList(buildingQueryWrapper);
                List<Integer> idList = new ArrayList();
                for (Building building : buildings){
                    idList.add(building.getId());
                }
                absentQueryWrapper.in("building_id", idList);
            }
            if(searchForm.getKey().equals("dormitoryName")) {
                QueryWrapper<Dormitory> dormitoryQueryWrapper = new QueryWrapper<>();
                dormitoryQueryWrapper.like("name", searchForm.getValue());
                List<Dormitory> dormitories = dormitoryMapper.selectList(dormitoryQueryWrapper);
                List<Integer> idList = new ArrayList();
                for (Dormitory dormitory : dormitories){
                    idList.add(dormitory.getId());
                }
                absentQueryWrapper.in("dormitory_id", idList);
            }
            page = absentMapper.selectPage(absentPage, absentQueryWrapper);

        }
        List<Absent> absents = absentPage.getRecords();
        ArrayList<AbsentVO> absentVOS = new ArrayList<>();
        for (Absent absent : absents) {
            AbsentVO absentVO = new AbsentVO();
            BeanUtils.copyProperties(absent, absentVO);
            Student student = studentMapper.selectById(absent.getStudentId());
            Dormitory dormitory = dormitoryMapper.selectById(absent.getDormitoryId());
            Building building = buildingMapper.selectById(absent.getBuildingId());
            DormitoryAdmin dormitoryAdmin = dormitoryAdminMapper.selectById(absent.getDormitoryAdminId());
            absentVO.setBuildingName(building.getName());
            absentVO.setDormitoryName(dormitory.getName());
            absentVO.setStudentName(student.getName());
            absentVO.setDormitoryAdminName(dormitoryAdmin.getName());
            absentVOS.add(absentVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setTotal(page.getTotal());
        pageVO.setData(absentVOS);
        return pageVO;
    }
}
